package com.banksoft.cps.service.impl;

import com.banksoft.cps.bean.*;
import com.banksoft.cps.bean.common.ServiceException;
import com.banksoft.cps.bean.query.SupplierIntoWarehouseQuery;
import com.banksoft.cps.bean.warpper.SupplierIntoWarehouseWarpper;
import com.banksoft.cps.constants.ErrorConstant;
import com.banksoft.cps.dao.*;
import com.banksoft.cps.service.SupplierIntoWarehouseService;
import com.banksoft.cps.util.DateUtil;
import com.banksoft.cps.util.UUIDFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by rongzhao.zhu on 2018/1/12.
 */
@Service
public class SupplierIntoWarehouseServiceImpl implements SupplierIntoWarehouseService {
    @Autowired
    SupplierOrderDao supplierOrderDao;
    @Autowired
    CompanyDao companyDao;

    @Autowired
    SupplierIntoWarehouseDao supplierIntoWarehouseDao;

    @Autowired
    private TransportCompanyDao transportCompanyDao;

    @Autowired
    MaterialDao materialDao;

    @Override
    public List<SupplierIntoWarehouseWarpper> list(SupplierIntoWarehouseQuery query) {
        List<SupplierIntoWarehouse> list = supplierIntoWarehouseDao.list(query);
        List<SupplierIntoWarehouseWarpper>  warehouseWarpperList = new ArrayList<>();
        for(SupplierIntoWarehouse a : list) {
            SupplierIntoWarehouseWarpper warehouseWarpper = new SupplierIntoWarehouseWarpper();
            BeanUtils.copyProperties(a, warehouseWarpper);
            SupplierOrder supplierOrder = supplierOrderDao.findById(a.getSupplierOrderId());
            if(supplierOrder != null) {
                warehouseWarpper.setSupplierOrderName(supplierOrder.getName());
                //查询品类
                Material material = materialDao.findById(supplierOrder.getMaterialId());
                if (material != null){
                    warehouseWarpper.setMaterialName(material.getName());
                }
            }

            Company company = companyDao.findById(a.getCompanyId());
            warehouseWarpper.setCompanyName(company == null ? null : company.getName());

            TransportCompany transportCompany = transportCompanyDao.findById(supplierOrder.getTransportCompanyId());
            warehouseWarpper.setTransportCompanyName(transportCompany == null ? null : transportCompany.getName());


            warehouseWarpperList.add(warehouseWarpper);
        }
        return warehouseWarpperList;
    }

    @Override
    public long count(SupplierIntoWarehouseQuery query) {
        return supplierIntoWarehouseDao.count(query);
    }

    @Override
    public void save(SupplierIntoWarehouse bean) throws ServiceException {
        if(StringUtils.isNotBlank(bean.getId())){
            supplierIntoWarehouseDao.update(bean);
        }else {
            bean.setId(UUIDFactory.random());
            bean.setCreateTime(System.currentTimeMillis());
            supplierIntoWarehouseDao.insert(bean);
        }
    }

    @Override
    public SupplierIntoWarehouse findById(String id) {
        return supplierIntoWarehouseDao.findById(id);
    }
}
